package com.apply.leetcode.puzzle387;

/**
 * 返回第一个不重复的字符的索引，不存在就返回-1
 * leetcode 返回0
 * loveleetcode 返回2
 *
 * 思路：统计每个字符串的频率，然后找到第一个频率为1的
 */
public class Solution {
    public int findFirstUniChar(String s) {
        int[] freq = new int[26];
        for (int i = 0 ; i < s.length() ; i++) {
            freq[s.charAt(i) - 'a'] ++;
        }
        for (int i = 0 ; i < s.length() ; i++) {
            if (freq[s.charAt(i) - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int uniCharIndex = solution.findFirstUniChar("loveleetcode");
        System.out.println(uniCharIndex);
    }
}
